
## monthly cost data -------------------------------
get_monthly_cost <- function(con) {
  

  time1 <- Sys.time()
  dt_original_mc <- data.table::as.data.table(
    DBI::dbGetQuery(
      con,
      paste0("sql_query"
      )
    )
  ) 
  time2 <- Sys.time()
  print("month cost cnr table extraction took (in mins):")
  print(time2-time1)
  rm(time2, time1)
  print("month cost cnr finished. Start pre-processing...")
  
  dt_mc_mainCat <- data.table::copy(dt_original_mc[, 
                                                   .(actual_cost = sum(actual_cost, na.rm = T),
                                                     amount = sum(amount, na.rm=T)),
                                                   by = .(id_var,index_date,months_after_dx,main_cat )])
  
  dt_mc <- data.table::copy(dt_original_mc[, 
                                           .(actual_cost = sum(actual_cost, na.rm = T),
                                             amount = sum(amount, na.rm=T)),
                                           by = .(id_var,index_date,months_after_dx )])
  
  dt_mc_adm_prof <- data.table::copy(dt_original_mc[main_cat%in% c("Inpatient_Planned",
                                                                   "Inpatient_Unplanned"),
                                                    .(actual_cost = sum(actual_cost, na.rm = T),
                                                      amount = sum(amount, na.rm=T)),
                                                    by = .(id_var,index_date,months_after_dx,
                                                           main_cat,profession )])
  
  
  dt_mc_adm_prof_all <- data.table::copy(dt_original_mc[main_cat%in% c("Inpatient_Planned",
                                                                       "Inpatient_Unplanned"),
                                                        .(actual_cost = sum(actual_cost, na.rm = T),
                                                          amount = sum(amount, na.rm=T)),
                                                        by = .(id_var,index_date,months_after_dx,
                                                               profession )])
  
  dt_mc[,months_after_dx_0:=months_after_dx-1]
  dt_mc_mainCat[,months_after_dx_0:=months_after_dx-1]
  dt_mc_adm_prof_all[,months_after_dx_0:=months_after_dx-1]
  dt_mc_adm_prof[,months_after_dx_0:=months_after_dx-1]
  
  
  return(list(dt_mc,dt_mc_mainCat ,dt_mc_adm_prof_all ,dt_mc_adm_prof ))
  }

